package com.liu.lab06.servlet;

import com.liu.lab06.pojo.Music;
import com.liu.lab06.utils.MybatsUtils;
import jakarta.servlet.*;
import jakarta.servlet.http.*;
import jakarta.servlet.annotation.*;
import org.apache.ibatis.session.SqlSession;

import java.io.IOException;
import java.util.List;

@WebServlet(name = "FindMusicServlet", value = "/FindMusic")
public class FindMusicServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=UTF-8");
        //1 获取参数
        String musicname=request.getParameter("musicname");// 音乐的名字
        int category_id =Integer.parseInt(request.getParameter("category_id")); //音乐的分类
        String singer=request.getParameter("singer"); // 歌手的名字
        Music m = new Music(); // 我们new一个music
        // 把获取到的数据压入我们的对象中
        m.setMusicname(musicname);
        m.setCategory_id(category_id);
        m.setSinger(singer);
//        获取查询的结果
        List<Music> userList = MybatsUtils.selectList("MusicMapper.findByN_C_S",m);

        //2 将查询到的音乐信息保存到session中
        HttpSession session = request.getSession();
        session.setAttribute("allMusics", userList);

        //3 页面跳转到 musics.jsp
        request.getRequestDispatcher("Music.jsp").forward(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request,response);
    }
}
